package com.example.bbs.dao.impl;

import com.example.bbs.dao.BaseDao;
import com.example.bbs.dao.CallBack;
import com.example.bbs.dao.UserDao;
import com.example.bbs.model.User;
import com.example.bbs.utils.TimeUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
public class UserDaoImpl extends BaseDao implements UserDao, CallBack {

    @Override
    public boolean doReg(User user) {

        String sql = "insert into user value(default,?,?,?,?,?,?)";

        return executeUpdate(sql,user.getUserName(),user.getUserPwd(),user.getUserHead(),user.getPostNumber(),user.getCreateTime(),user.getLoginTime());
    }

    @Override
    public User findUserByNameAndPwd(User user) {
        String sql = "select * from user where user_name = ? and user_pwd = ?";
        List<Object> lists = executeQuery(this,sql,user.getUserName(),user.getUserPwd());
        if(lists.size() == 1){
            return (User)lists.get(0);
        }
        return null;
    }

    @Override
    public User findUserById(int userId) {
        String sql = "select * from user where user_id = ?";
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement(sql);
            //填坑
            statement.setInt(1, userId);
            //执行
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return  new User(resultSet.getInt(1), resultSet.getString(2),resultSet.getString(3),resultSet.getString(4), resultSet.getInt(5),TimeUtil.getStringToDate(resultSet.getString(6)),TimeUtil.getStringToDate(resultSet.getString(7)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return null;
    }

    @Override
    public User findUserByPostId(int postId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from user INNER JOIN post on post.user_id = `user`.user_id  where post_id = ?");
            //填坑
            statement.setInt(1, postId);
            //执行
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return  new User(resultSet.getInt(1), resultSet.getString(2),resultSet.getString(3),resultSet.getString(4), resultSet.getInt(5),TimeUtil.getStringToDate(resultSet.getString(6)),TimeUtil.getStringToDate(resultSet.getString(7)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return null;
    }

    @Override
    public boolean updatePwd(User user, String newPwd) {
        String sql = "update user set user_pwd = ? where user_id = ?";
        return executeUpdate(sql,newPwd,user.getUserId());
    }

    @Override
    public boolean updateLoginTime(int userId, Date lnginTime) {
        String sql = "update user set login_time = ? where user_id = ?";
        return executeUpdate(sql,lnginTime,userId);
    }

    @Override
    public User findUserByName(User user) {
        String sql = "select * from user where user_name = ?";
        List<Object> lists = executeQuery(this,sql,user.getUserName());
        if(lists.size() == 1){
            return (User)lists.get(0);
        }
        return null;
    }

    @Override
    public List<Object> doResultSet(ResultSet resultSet) {
        List<Object> lists = new ArrayList<>();
        User user = null;
        while(true){
            try {
                if (!resultSet.next()) break;
                try {
                    user = new User(resultSet.getInt(1), resultSet.getString(2),resultSet.getString(3),resultSet.getString(4), resultSet.getInt(5),TimeUtil.getStringToDate(resultSet.getString(6)),TimeUtil.getStringToDate(resultSet.getString(7)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            lists.add(user);
        }

        return lists;
    }
}
